package com.dodol.runner;

import com.dodol.common.*;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;


import java.util.List;

/**
 * 解析测试用例集合，运行测试
 */
public class TestCaseRunner {


    private BaseTestCase testCase;
    private WebDriver driver;

    public TestCaseRunner(BaseTestCase testCase, WebDriver driver) {
        this.testCase = testCase;
        this.driver = driver;
    }

    /**
     * 运行测试
     */
    public void run() {

        if (testCase != null) {
            List<TestStep> steps = testCase.getStep();
            // 迭代测试步骤对象，根据Action类型运行
            for (TestStep step : steps) {
                // 测试动作，例如一个点击动作，开启浏览器动作
                UserAction userAction = step.getUserActionType();
                switch (userAction) {
                    case OPEN:
                        driver.get(step.getEnterValue());
                        Reporter.log("打开：" + step.getEnterValue());
                        break;
                    case CLICK:
                        UserAction.MouseAction.clickElement(driver, step);
                        Reporter.log("点击：" + step.getElement().getValue());
                        break;
                    case CLEAR:
                        UserAction.KeyboardAction.clearElement(driver, step);
                        Reporter.log("清空内容");
                        break;
                    case PRESS_KEY:
                        UserAction.KeyboardAction.pressKeyInElement(driver, step);
                        Reporter.log("输入：" + step.getEnterValue());
                        break;
                    case BACK:
                        driver.navigate().back();
                        Reporter.log("返回上一页");
                        break;
                    case CLOSE:
                        driver.close();
                        Reporter.log("关闭浏览器");
                        break;
                    case FORWARD:
                        driver.navigate().forward();
                        Reporter.log("前进");
                    case REFRESH:
                        driver.navigate().refresh();
                        Reporter.log("刷新浏览器");
                    case SUBMIT:
                        UserAction.KeyboardAction.submitElement(driver, step);
                        Reporter.log("提交内容");
                        break;
                    case CLICK_AND_HOLD:
                        UserAction.MouseAction.clickAndHoldElement(driver, step);
                        break;
                    case DRAG_DROP:
                        // todo 拖动操作需要从某个元素到某个元素
                        //UserAction.MouseAction.dragDropElement(driver,step);
                        break;
                    case DOUBLE_CLICK:
                        UserAction.MouseAction.doubleClickElement(driver, step);
                        break;
                    case CONTEXT_CLICK:
                        UserAction.MouseAction.contextClickElement(driver, step);
                        break;
                    case SELECT:
                        UserAction.MouseAction.selectElement(driver, step);
                        break;
                }

                // 校验检查点
                ExpectedVerification verification = step.getVerification();
                if (verification != null) {
                    verification.verity();
                    System.out.println(verification + "--检查点验证通过");
                } else {
                    System.out.println("无检查点，跳过此步！");
                }

                // 每个步骤间隔1s
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}
